package base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class Configuration extends JFrame {
    public static HashMap<String,String> standardTypeMapper = new HashMap<>();
    public static HashMap<String,String> userTypeMapper;
    public static String[] getTableSqls = {"show tables", "select name from sysobjects where xtype='u'"};
    public static String[] getViewSqls = {"show table status where comment='view'", "select name from sysobjects where xtype = 'v'"};

    static {
        standardTypeMapper.put("VARCHAR","java.lang.String");
        standardTypeMapper.put("CHAR","java.lang.Character");
        standardTypeMapper.put("BLOB","java.lang.Byte[]");
        standardTypeMapper.put("TINYBLOB","java.lang.Byte[]");
        standardTypeMapper.put("LONGBLOB","java.lang.Byte[]");
        standardTypeMapper.put("TEXT","java.lang.String");
        standardTypeMapper.put("TINYTEXT","java.lang.String");
        standardTypeMapper.put("LONGTEXT","java.lang.String");
        standardTypeMapper.put("BINARY","java.lang.Byte[]");
        standardTypeMapper.put("VARBINARY","java.lang.Byte[]");
        standardTypeMapper.put("MEDIUMBLOB","java.lang.Byte[]");
        standardTypeMapper.put("MEDIUMTEXT","java.lang.String");
        standardTypeMapper.put("NCHAR","java.lang.String");
        standardTypeMapper.put("NTEXT","java.lang.String");
        standardTypeMapper.put("NVARCHAR","java.lang.String");
        standardTypeMapper.put("IMAGE","java.lang.Byte[]");
        standardTypeMapper.put("UNIQUEIDENTIFIER","java.lang.String");

        standardTypeMapper.put("INT","java.lang.Integer");
        standardTypeMapper.put("FLOAT","java.lang.Float");
        standardTypeMapper.put("DOUBLE","java.lang.Double");
        standardTypeMapper.put("BIGINT","java.lang.Long");
        standardTypeMapper.put("SMALLINT","java.lang.Integer");
        standardTypeMapper.put("BIT","java.lang.Boolean");
        standardTypeMapper.put("TINYINT","java.lang.Integer");
        standardTypeMapper.put("MEDIUMINT","java.lang.Integer");
        standardTypeMapper.put("DECIMAL","java.math.BigDecimal");
        standardTypeMapper.put("NUMERIC","java.math.BigDecimal");
        standardTypeMapper.put("MONEY","java.math.BigDecimal");
        standardTypeMapper.put("SMALLMONEY","java.math.BigDecimal");
        standardTypeMapper.put("REAL","java.lang.Float");

        standardTypeMapper.put("TIME","java.sql.Time");
        standardTypeMapper.put("TIMESTAMP","java.sql.Timestamp");
        standardTypeMapper.put("DATE","java.sql.Date");
        standardTypeMapper.put("DATETIME","java.time.LocalDateTime");
        standardTypeMapper.put("YEAR","java.sql.Date");
        standardTypeMapper.put("DATETIME2","java.sql.Timestamp");
        standardTypeMapper.put("DATETIMEOFFSET","microsoft.sql.DateTimeOffset");
        standardTypeMapper.put("SMALLDATETIME","java.sql.Timestamp");


        userTypeMapper = new HashMap<>(standardTypeMapper);
    }

    public Configuration(){
        setSize(new Dimension(420,600));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
        JPanel top = new JPanel();
        top.setLayout(new FlowLayout(FlowLayout.LEFT));
        getContentPane().add(top,BorderLayout.NORTH);
        JLabel label = new JLabel("可修改数据库字段类型对应的java类型");
        label.setFont(new Font("微软雅黑",Font.PLAIN,18));
        top.add(label);

        JScrollPane jScrollPane = new JScrollPane();
        getContentPane().add(jScrollPane,BorderLayout.CENTER);
        JPanel center = new JPanel();
        center.setLayout(new GridLayout(standardTypeMapper.size(),1,10,10));
        jScrollPane.setViewportView(center);

        ArrayList<Item> items = new ArrayList<>();
        ArrayList<Map.Entry<String, String>> entries = new ArrayList<>(userTypeMapper.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        for(Map.Entry<String,String> entry : entries){
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            JLabel label1 = new JLabel(entry.getKey());
            label1.setFont(new Font("微软雅黑",Font.PLAIN,17));
            panel.add(label1,BorderLayout.WEST);
            JTextField jTextField = new JTextField(entry.getValue());
            jTextField.setPreferredSize(new Dimension(220,30));
            jTextField.setFont(new Font("微软雅黑",Font.PLAIN,17));
            panel.add(jTextField,BorderLayout.EAST);
            center.add(panel);
            items.add(new Item(label1,jTextField));
        }

        JPanel bottom = new JPanel();
        bottom.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
        getContentPane().add(bottom,BorderLayout.SOUTH);
        JButton jButton = new JButton("保存");
        jButton.setFocusPainted(false);
        jButton.setFont(new Font("微软雅黑",Font.PLAIN,16));
        JButton jButton1 = new JButton("恢复默认配置");
        jButton1.setFont(new Font("微软雅黑",Font.PLAIN,16));
        jButton1.setFocusPainted(false);
        bottom.add(jButton1);
        bottom.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(Item item : items){
                    String text = item.textField.getText();
                    if(text != null || text.length() != 0){
                        userTypeMapper.put(item.label.getText(),text);
                    }
                }
                JOptionPane.showMessageDialog(null, "保存成功", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(Item item : items){
                    item.textField.setText(standardTypeMapper.get(item.label.getText()));
                }
            }
        });
    }

    private static class Item{
        public JLabel label;
        public JTextField textField;
        public Item(JLabel label, JTextField textField){
            this.label = label;
            this.textField = textField;
        }
    }

}
